<template>
  <div class="app-container">
    <el-form ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="产品项目">
        <el-input
          v-model="queryParams.productItems"
          placeholder="请输入产品项目"
        >
          <el-button slot="append" size="small">搜索</el-button>
        </el-input>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      border
      :data="projectList"
      @filter-change="handleFilterChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="病种"
        align="center"
        prop="firstProjiect"
        column-key="firstProjiect"
        :filters="entityOptions"
      />
      <el-table-column
        label="类别"
        align="center"
        prop="type"
        column-key="type"
        :filters="chargingProjectsTypeOptions"
      />
      <el-table-column label="产品项目" align="center" prop="productItems" />
      <el-table-column
        label="标签"
        align="center"
        prop="label"
        column-key="label"
        :filters="chargingProjectsTagOptions"
      >
      </el-table-column>
      <el-table-column label="价格" align="center">
        <template slot-scope="scope">
          {{ scope.row.price1 }} ~ {{ scope.row.price2 }}
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column
        label="开单状态"
        align="center"
        prop="billingStatus"
        column-key="billingStatus"
        :filters="clinchDealBasisOptions"
      />
      <el-table-column
        label="是否上架"
        align="center"
        prop="isUp"
        column-key="isUp"
        width="100"
        :filters="shelvesStatusOptions"
      >
        <template slot-scope="scope">
          {{ shelvesStatusEnum[scope.row.isUp] }}
        </template>
      </el-table-column>
      <el-table-column
        label="前台可开单"
        align="center"
        prop="isBilling"
        min-width="120"
        column-key="isBilling"
        width="120"
        :filters="frontDeskOrderOptions"
      >
        <template slot-scope="scope">
          {{ frontDeskOrderEnum[scope.row.isBilling] }}
        </template>
      </el-table-column>
      <el-table-column
        label="是否免费"
        align="center"
        prop="isFree"
        min-width="120"
        column-key="isFree"
      >
        <template slot-scope="scope">
          {{ whetherFreeEnum[scope.row.isFree] }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listProject } from '@/api/erp/project'
import {
  entityOptions,
  shelvesStatusOptions,
  frontDeskOrderOptions,
  whetherFreeOptions,
  clinchDealBasisOptions,
  chargingProjectsTypeOptions,
  chargingProjectsTagOptions,
} from '@/utils/options'

import {
  entityEnum,
  shelvesStatusEnum,
  frontDeskOrderEnum,
  whetherFreeEnum,
  clinchDealBasisEnum,
  chargingProjectsTypeEnum,
  chargingProjectsTagEnum,
} from '@/utils/enums'

const FROM_DATA = {
  id: undefined,
  type: undefined,
  productItems: undefined,
  label: undefined,
  price1: undefined,
  price2: undefined,
  unit: undefined,
  billingStatus: undefined,
  isUp: undefined,
  isBilling: undefined,
  isFree: undefined,
  remarks: undefined,
}

const QUERY_PARAMS = {
  pageNum: 1,
  pageSize: 10,
  firstProjiect: undefined,
  type: undefined,
  productItems: undefined,
  label: undefined,
  billingStatus: undefined,
  isUp: undefined,
  isBilling: undefined,
  isFree: 1,
}

export default {
  name: 'ChargeProject',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 收费项目表格数据
      projectList: [],
      // 查询参数
      queryParams: {},
      // 表单参数
      form: {},
    }
  },
  computed: {
    entityOptions() {
      return entityOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    shelvesStatusOptions() {
      return shelvesStatusOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    frontDeskOrderOptions() {
      return frontDeskOrderOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    whetherFreeOptions() {
      return whetherFreeOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    clinchDealBasisOptions() {
      return clinchDealBasisOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    chargingProjectsTypeOptions() {
      return chargingProjectsTypeOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    chargingProjectsTagOptions() {
      return chargingProjectsTagOptions.map(({ label, value }) => ({
        text: label,
        label,
        value,
      }))
    },
    entityEnum() {
      return entityEnum
    },
    shelvesStatusEnum() {
      return shelvesStatusEnum
    },
    frontDeskOrderEnum() {
      return frontDeskOrderEnum
    },
    whetherFreeEnum() {
      return whetherFreeEnum
    },
    clinchDealBasisEnum() {
      return clinchDealBasisEnum
    },
    chargingProjectsTypeEnum() {
      return chargingProjectsTypeEnum
    },
    chargingProjectsTagEnum() {
      return chargingProjectsTagEnum
    },
  },
  mounted() {
    this.queryParams = { ...QUERY_PARAMS }
    this.getList()
  },
  methods: {
    /** 查询收费项目列表 */
    getList() {
      this.loading = true
      listProject(this.queryParams).then((response) => {
        this.projectList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    handleFilterChange(e) {
      const [key, value] = Object.entries(e)[0]
      this.queryParams[key] = value.join(',')
      this.handleQuery()
    },
  },
}
</script>
